import {editdizhi,deldizhi} from '../../utils/http'

Page({
  data: {
    //弹窗
    show: false,
    show1: false,
    show2: false,
    city: null,
    citys: null,
    cityes: null,
    pid: null,
    pid1: null,
    sf: '',
    cs: '',
    xq: '',
    // 个人信息
    linkMan:'',
    mobile:'',
    address:'',
    id:'',
    },
  onLoad: function (options) {
      console.log(options.list)
      if(options.list==null){
          JSON.stringify(options.list)
      }else{
          let res=JSON.parse(options.list)
          console.log(res)
          this.setData({sf:res.cityStr,cs:res.areaStr,linkMan:res.linkMan,mobile:res.mobile,address:res.address,id:res.id})
      }
    let that = this
    // 省会
    wx.request({
      url: 'https://api.it120.cc/common/region/v2/province', //仅为示例，并非真实的接口地址
      success(res) {
        that.setData({ city: res.data.data })
      }
    })
  },
  // 显示隐藏弹窗
  showPopup() {
  
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  showPopup1() {
    this.setData({ show1: true });
  },
  onClose1() {
    this.setData({ show1: false });
  },
  showPopup2() {
    this.setData({ show2: true });
  },
  onClose2() {
    this.setData({ show2: false });
  },
  onConfirm(e) {
    this.setData({ sf: e.detail.value.name, pid: e.detail.value.id })
    let that = this
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${e.detail.value.id}`, //仅为示例，并非真实的接口地址
      success(res) {
        console.log(res.data.data)
        that.setData({ citys: res.data.data })
      }
    })
    this.onClose()
  },
  // 区
  onConfirm1(e) {
    this.setData({ cs: e.detail.value.name, pid1: e.detail.value.id })
    let that = this
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${e.detail.value.id}`, //仅为示例，并非真实的接口地址
      success(res) {
        console.log(res.data.data)
        that.setData({ cityes: res.data.data })
      }
    })
    this.onClose1()
  },
  onConfirm2(e) {
    this.setData({ qx: e.detail.value.name })
    this.onClose2()
  },
  // 收货人
  onChange1(e){
    console.log(e.detail)
    this.setData({linkMan:e.detail})
  },
  onChange2(e){
    console.log(e.detail)
    this.setData({mobile:e.detail})
  },
  onChange3(e){
    console.log(e.detail)
    this.setData({address:e.detail})
  },
  //删除
  delfrom(e){
      console.log(e.currentTarget.dataset.id)
      let id=e.currentTarget.dataset.id
      let token=wx.getStorageSync('token')
      console.log(token)
      deldizhi(id,token).then(res=>{
          console.log(res)
          wx.navigateTo({
            url: '/pages/my_cargo/my_cargo'
          })
      })

  },
  //修改地址 
  addfrom(){
    let token=wx.getStorageSync('token')
    let linkMan=this.data.linkMan
    let mobile=this.data.mobile
    let address=this.data.address
    let cityId=this.data.pid
    let provinceId=this.data.pid1
    let id=this.data.id

    let obj={
      token,linkMan,mobile,address,cityId,provinceId,provinceId,id
    }
    editdizhi(obj).then(res=>{
        console.log('修改中')
            wx.reLaunch({
              url: '/pages/my_cargo/my_cargo',
            })
    })
  }
})